package com.dailyblue.java.spring.rocketmq.普通消息.消息生产;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

public class 异步消息的生产 {
    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("zhuoyue01");
        producer.setNamesrvAddr("192.168.160.88:9876");
        producer.start();
        for (int i = 1; i < 11; i++) {
            String topic = "TopicA";
            String tags = "yibu";
            String body = "ASYNC,index:" + i;
            Message message = new Message(topic, tags, body.getBytes(StandardCharsets.UTF_8));
            producer.send(message, new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    System.out.println(sendResult);
                }

                @Override
                public void onException(Throwable throwable) {
                    System.out.println(throwable.getMessage());
                }
            });
        }
        Thread.sleep(3000);
        producer.shutdown();
    }
}
